package com.wwu.system.controller;

import com.wwu.common.constant.CommConst;
import com.wwu.common.enums.MenuBtnEnum;
import com.wwu.common.web.ResultObj;
import com.wwu.common.web.ResultPage;
import com.wwu.system.common.aspect.Log;
import com.wwu.system.common.security.SecurityUtil;
import com.wwu.system.entity.SysUser;
import com.wwu.system.service.ISysUserService;
import io.swagger.annotations.*;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import java.util.Arrays;

/**
 * 系统用户前端控制器
 * @author 一蓑烟雨
 * @date 2021-10-26
 * @version 1.0.0
 */
@Api(tags = "系统用户控制器(SysUserController)")
@ApiSort(3)
@Controller
@RequestMapping("/system/sysUser")
public class SysUserController {
    @Resource
    private ISysUserService sysUserService;

    /**
     * 请求转发用户个人信息页面
     * @return java.lang.String
     * @author 一蓑烟雨
     * @date 2023/6/2 16:31
     */
    @GetMapping("/userSetting")
    public String userSettingHtml(Model model) {
        //传递当前用户信息属性
        model.addAttribute(CommConst.SYS_USER, SecurityUtil.getCurrUser());
        return "system/sysuser/user-setting";
    }

    /**
     * 请求转发用户密码修改页面
     * @return java.lang.String
     * @author 一蓑烟雨
     * @date 2023/6/2 16:31
     */
    @GetMapping("/userPassword")
    public String userPasswordHtml() {
        return "system/sysuser/user-password";
    }

    /**
     * 请求转发用户管理主页面
     * @return java.lang.String
     * @author 一蓑烟雨
     * @date 2023/6/2 16:31
     */
    @GetMapping("/userMain")
    public String userMainHtml() {
        return "system/sysuser/user-main";
    }

    /**
     * 请求系统用户新增或更新页面
     * @param userId
     * @return java.lang.String
     * @author 一蓑烟雨
     * @date 2023/6/12 20:10
     */
    @GetMapping("/userAddOrUpdate")
    public String userAddOrUpdateHtml(String userId,Model model){
        if(StringUtils.hasText(userId)){
            model.addAttribute(CommConst.SYS_USER,sysUserService.getById(userId));
        }
        return "system/sysuser/user-add-edit";
    }

    /**
     * 请求系统用户角色新增或更新页面
     * @param userId
     * @return java.lang.String
     * @author 一蓑烟雨
     * @date 2023/6/12 20:10
     */
    @GetMapping("/userRoleAddOrUpdate")
    public String userRoleAddOrUpdateHtml(String userId, Model model){
        if(StringUtils.hasText(userId)){
            model.addAttribute(CommConst.SYS_USER,sysUserService.getById(userId));
        }
        return "system/sysuser/user-role-edit";
    }

    @Log(menuName = "用户管理",menuBtnEnum = MenuBtnEnum.SHOW)
    @ApiOperation(value = "分页查询用户信息")
    @ApiOperationSupport(order = 1)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page", value = "当前页数",paramType = "query", required = true),
            @ApiImplicitParam(name = "limit", value = "每页条数",paramType = "query", required = true),
            @ApiImplicitParam(name = "sysUser", value = "系统用户实体")
    })
    @GetMapping("/userList")
    @ResponseBody
    public ResultPage<SysUser> userList(@RequestParam(defaultValue = "1") Integer page,
                                        @RequestParam(defaultValue = "5000") Integer limit,
                                        SysUser sysUser){
        return sysUserService.getUserListPage(page,limit,sysUser);
    }

    @Log(menuName = "用户管理",menuBtnEnum = MenuBtnEnum.ADD)
    @ApiOperation(value = "添加用户信息")
    @ApiOperationSupport(order = 2)
    @PostMapping("/saveUser")
    @ResponseBody
    public  ResultObj saveUser(@RequestBody SysUser sysUser){
        sysUserService.saveUser(sysUser);
        return ResultObj.success("用户信息添加成功");
    }

    @Log(menuName = "用户管理",menuBtnEnum = MenuBtnEnum.EDIT)
    @ApiOperation(value = "更新用户信息")
    @ApiOperationSupport(order = 3)
    @PutMapping("/updateUser")
    @ResponseBody
    public ResultObj updateUser(@RequestBody SysUser sysUser){
        sysUserService.updateUser(sysUser);
        return ResultObj.success("用户信息更新成功");
    }

    @Log(menuName = "用户管理",menuBtnEnum = MenuBtnEnum.UPDATE)
    @ApiOperation(value = "更新用户密码")
    @ApiOperationSupport(order = 4)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "oldPwd", value = "原密码", required = true),
            @ApiImplicitParam(name = "newPwd", value = "新密码", required = true),
            @ApiImplicitParam(name = "confirmPwd", value = "确认密码", required = true)
    })
    @PutMapping("/updateUserPwd")
    @ResponseBody
    public ResultObj updateUserPwd(String oldPwd,String newPwd,String confirmPwd){
        sysUserService.updateUserPwd(SecurityUtil.getCurrUserName(),oldPwd,newPwd,confirmPwd);
        return ResultObj.success("用户密码更新成功");
    }

    @Log(menuName = "用户管理",menuBtnEnum = MenuBtnEnum.DEL)
    @ApiOperation(value = "删除用户信息")
    @ApiOperationSupport(order = 5)
    @DeleteMapping("/deleteUser/{userIds}")
    @ResponseBody
    public ResultObj deleteUser(@ApiParam(name = "userIds", value = "用户id集合，以逗号分隔", required = true)
                                    @PathVariable String userIds){
        sysUserService.deleteUser(Arrays.asList(userIds.split(CommConst.SPLIT_COMMA)));
        return ResultObj.success("用户信息删除成功");
    }
}